define(['app'],function(app){return {module: function($scope, $http, eventBusService, httpService, params, configService) {
		$scope.form.fk_app = params.id;
		
        $scope.unitBTyps = [];
        $scope.unitLTyps = [];
        var addBtype = function(bType){
            var isH = true;
            for(var i=0;i<$scope.unitBTyps.length;i++){
                if(bType.dictionary_pk === $scope.unitBTyps[i].dictionary_pk){
                    isH = false;
                    break;
                }
            }
            if(isH){
                $scope.unitBTyps[$scope.unitBTyps.length] = bType;
            }
        }

        var addLtype = function(bType){
            var isH = true;
            for(var i=0;i<$scope.unitLTyps.length;i++){
                if(bType.dictionary_pk === $scope.unitLTyps[i].dictionary_pk && bType.btype_pk === $scope.unitLTyps[i].btype_pk){
                    isH = false;
                    break;
                }
            }
            if(isH){
                $scope.unitLTyps[$scope.unitLTyps.length] = bType;
            }
        }   
        
    	//查询单位
    	httpService.post(app.urls.unit_selectUnitList,{"fk_app":$scope.form.fk_app},function(data){
    		if("0000" === data.code){
    			 $scope.unitList = data.data;
    			 
                 for(var i = 0; i < $scope.unitList.length; i++){
                     var unit = $scope.unitList[i];

                     var unitBTyp = {};
                     unitBTyp.dictionary_pk = unit.fk_dictionary_ascertained;
                     unitBTyp.dictionary_name = unit.dictionary_ascertained_name;
                     addBtype(unitBTyp);

                     var unitLTyp = {};
                     unitLTyp.dictionary_pk = unit.fk_dictionary_school_type;
                     unitLTyp.dictionary_name = unit.dictionary_school_type_name;
                     unitLTyp.btype_pk = unit.fk_dictionary_ascertained;
                     addLtype(unitLTyp);
                 }
                 $scope.$apply();

                 for(var i = 0; i < $scope.unitList.length; i++){
                	 if($scope.unitList[i].checked == '1') {
                		 $('#'+$scope.unitList[i].id).addClass('active');
                	 }
                 }
    		}else{
    			alert(data.msg);
    		}
    	});
        $scope.setUnit = function($event){
            if($($event.target).hasClass('active')){
                $($event.target).removeClass('active');
            }else {
                $($event.target).addClass('active');
            }
        }
        $scope.selectAll = function($event,num){
            if($($event.target).hasClass('active')){
                $($event.target).removeClass('active');
                if (num==0){
                    $($event.target).parent("p").next(".checkcon").find("span").removeClass('active');
                }else {
                    $($event.target).parent("p").next("div").children("span").removeClass('active');
                }
            }else {
                $($event.target).addClass('active');
                $($event.target).siblings('span').removeClass('active');
                if (num==0) {
                    $($event.target).parent("p").next(".checkcon").find("span").addClass('active');
                }else {
                    $($event.target).parent("p").next("div").children("span").addClass('active');

                }
            }
        }
        $scope.selectOpposite = function($event,num){
            if($($event.target).hasClass('active')){
                $($event.target).removeClass('active');
            }else {
                $($event.target).addClass('active');
                $($event.target).siblings('span').removeClass('active');
            }
            if (num==0) {
                $($event.target).parent("p").next(".checkcon").find("span").each(function () {
                    if ($(this).hasClass('active')) {
                        $(this).removeClass('active');
                    } else {
                        $(this).addClass('active');
                    }
                });
            }else {
                $($event.target).parent("p").next("div").children("span").each(function () {
                    if ($(this).hasClass('active')) {
                        $(this).removeClass('active');
                    } else {
                        $(this).addClass('active');
                    }
                });
            }
        }
        //设置数据
        $scope.saveForm = function(){
            var units = [];
            $(".checkcon span").each(function () {
                if ($(this).hasClass('active')) {
                    units.push($(this).attr('id'));
                }
            });
            
            if(units.length==0){
               alert("至少选择一个单位");
            }else{
            	$scope.form.units = JSON.stringify(units);
	            httpService.post(app.urls.app_insertAppUnit,$scope.form,function(data) {
	                if ("0000" === data.code) {
	                    $scope.back();
	                } else {
	                    alert(data.msg);
	                }
	            });
            }
        }
      //返回
        $scope.back = function(){
            var menu = {};
            menu.url = "modules/content/syssetting/appsetting?father_id="+params.father_id;
            eventBusService.publish("modules.content.load",menu);
        }
    }}});